trimwhitespace {C}


파이썬 strip 같은 ν•¨μˆ˜κ°€ ν•„μš”ν–ˆλ‹€. κ·Έλž˜μ„œ κ²€μƒ‰ν•΄λ³΄λ‹ˆ strip_leftλŠ” λ‹¨μˆœνžˆ 리턴 포인터λ₯Ό λ°”κΏ”μ„œ ν•΄κ²°ν–ˆκ³ , strip_rightλŠ” λ’€μ—μ„œλΆ€ν„° μˆœνšŒν•˜λ©° ν™”μ΄νŠΈμŠ€νŽ˜μ΄μŠ€κ°€ μ•„λ‹Œ μœ„μΉ˜ +1에 \0을 λ‹¬μ•„μ„œ 문제λ₯Ό ν•΄κ²°ν–ˆλ‹€.

// Note: This function returns a pointer to a substring of the original string.
// If the given string was allocated dynamically, the caller must not overwrite
// that pointer with the returned value, since the original pointer must be
// deallocated using the same allocator with which it was allocated.  The return
// value must NOT be deallocated using free() etc.
char *trimwhitespace(char *str)
{
  char *end;

  // Trim leading space
  while(isspace((unsigned char)*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace((unsigned char)*end)) end--;

  // Write new null terminator character
  end[1] = '\0';

  return str;
}